package cn.iocoder.yudao.module.lesson.controller.admin.lesson;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.lesson.controller.admin.lesson.vo.*;
import cn.iocoder.yudao.module.lesson.dal.dataobject.lesson.LessonDO;
import cn.iocoder.yudao.module.lesson.convert.lesson.LessonConvert;
import cn.iocoder.yudao.module.lesson.service.lesson.LessonService;

@Tag(name = "管理后台 - 课程管理（针灸，内经，神龙本草，伤寒，金匮）")
@RestController
@RequestMapping("/qhsx/lesson")
@Validated
public class LessonController {

    @Resource
    private LessonService lessonService;

    @PostMapping("/create")
    @Operation(summary = "创建课程管理（针灸，内经，神龙本草，伤寒，金匮）")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson:create')")
    public CommonResult<Long> createLesson(@Valid @RequestBody LessonCreateReqVO createReqVO) {
        return success(lessonService.createLesson(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程管理（针灸，内经，神龙本草，伤寒，金匮）")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson:update')")
    public CommonResult<Boolean> updateLesson(@Valid @RequestBody LessonUpdateReqVO updateReqVO) {
        lessonService.updateLesson(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程管理（针灸，内经，神龙本草，伤寒，金匮）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qhsx:lesson:delete')")
    public CommonResult<Boolean> deleteLesson(@RequestParam("id") Long id) {
        lessonService.deleteLesson(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程管理（针灸，内经，神龙本草，伤寒，金匮）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson:query')")
    public CommonResult<LessonRespVO> getLesson(@RequestParam("id") Long id) {
        LessonDO lesson = lessonService.getLesson(id);
        return success(LessonConvert.INSTANCE.convert(lesson));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程管理（针灸，内经，神龙本草，伤寒，金匮）列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson:query')")
    public CommonResult<List<LessonRespVO>> getLessonList(@RequestParam("ids") Collection<Long> ids) {
        List<LessonDO> list = lessonService.getLessonList(ids);
        return success(LessonConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程管理（针灸，内经，神龙本草，伤寒，金匮）分页")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson:query')")
    public CommonResult<PageResult<LessonRespVO>> getLessonPage(@Valid LessonPageReqVO pageVO) {
        PageResult<LessonDO> pageResult = lessonService.getLessonPage(pageVO);
        return success(LessonConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程管理（针灸，内经，神龙本草，伤寒，金匮） Excel")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson:export')")
    @OperateLog(type = EXPORT)
    public void exportLessonExcel(@Valid LessonExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LessonDO> list = lessonService.getLessonList(exportReqVO);
        // 导出 Excel
        List<LessonExcelVO> datas = LessonConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课程管理（针灸，内经，神龙本草，伤寒，金匮）.xls", "数据", LessonExcelVO.class, datas);
    }

}
